from itsdangerous import URLSafeTimedSerializer as Serializer
from django.conf import settings
# 使用URLSafeTimedSerializer替代旧版本的Serializer
# 加密
def generic_openid(open_id):
    """对openid进行加密，生成access_token"""
    s = Serializer(settings.SECRET_KEY, salt='oauth')
    access_token = s.dumps({'openid': open_id})
    # 创建token，有效期300秒
    return s.loads(access_token)

#解密
def check_access_token(token):
    s= Serializer(settings.SECRET_KEY, salt='oauth')
    try:
        result = s.loads(token)
    except Exception:
        return None
    else:
        return result.get('openid')
